<?php
/**
 * Class Home_Page_Home
 */
class Admin_Page_Home extends Admin_Page_Abstract
{

    private static $_rules = array(
        'name'   => array(
            'name'  => '用户名',
            'rules' => array(
                'required' => array('messge' => '请输入 {name} '),
            )
        ),
        'gender' => [
            'name'  => '性别',
            'rules' => [
                'required' => array('messge' => '{name}不能为空'),
            ]
        ],
        'age'    => [
            'name'  => '年龄',
            'rules' => [
                'required' => array('messge' => '{name}不能为空'),
                'number'   => array('min' => 0, 'max' => 150, 'message' => '{name}必须为大于等于{min}小于等于{max}的数字'),
            ]
        ],
        'nation' => array(
            'name'  => '民族',
            'rules' => array(
                'required' => array('messge' => '请输入 {name} '),
            )
        ),
        'number' => array(
            'name'  => '手机号',
            'rules' => array(
                'required' => array('messge' => '请输入 {name} '),
                'mobile'   => array('message' => '手机号格式错误')
            )
        ),
    );

    /*
     *用户列表页面
     */
    public function doHome(Q_Request $input, Q_Response $output)
    {
        $list         = Q_DAL_Client::instance()->call('Admin_Home', 'Gets');
        $output->list = $list;

        $output->setTemplate();
    }

    /*
     * 删除数据(逻辑删除)
     */
    public function doDel(Q_Request $input, Q_Response $output)
    {
        $userId = $input->getInt('userId');

        $delete = Q_DAL_Client::instance()->call('Admin_Home', 'Delete', array('id' => $userId));

        return $delete;
    }

    public function doAdd(Q_Request $input, Q_Response $output)
    {
        $vali = Q_Validate::instance()->setRules(self::$_rules);

        $param = $vali->getParams('data');

        if (empty($param)) {
            $param = ['age' => 18, 'gender' => '女', 'nation' => '汉族'];
        }

        $vali->setParams($param);

        $output->vali = $vali;
        $output->setTemplate('~/From');
    }


    public function doEdit(Q_Request $input, Q_Response $output)
    {
        $userId = $input->getInt('userId');

        if ($userId < 1) {
            $this->showMsg(array('msg' => '不合法的id值！', 'callbackUrl' => '/?c=' . $input->ctlName . '&a=Edit'));
        }

        $vali  = Q_Validate::instance()->setRules(self::$_rules);
        $param = $vali->getParams();

        if (empty($param)) {
            $param = Q_DAL_Client::instance()->call('Admin_Home', 'Get', [
                'id' => $userId
            ]);
        }

        $vali->setParams($param);

        $output->vali = $vali;
        $output->user = $param;

        $output->setTemplate('~/From');
    }

    public function execAdd(Q_Request $input, Q_Response $output)
    {
        if ($this->_save($input, $output)) {
            self::showMsg(['msg' => '', 'status' => self::STATUS_SUCCESS, 'callbackUrl' => "/?c={$input->ctlName}&a=Home"]);
        }
    }

    public function execEdit(Q_Request $input, Q_Response $output)
    {
        if ($this->_save($input, $output)) {
            self::showMsg(['msg' => '', 'status' => self::STATUS_SUCCESS, 'callbackUrl' => "/?c={$input->ctlName}&a=Home"]);
        }
    }


    private function _save(Q_Request $input, Q_Response $output)
    {
        $param = $input->post('data');

        $vali = Q_Validate::instance()->setRules(self::$_rules)->setParams($param);

        if (!$vali->validate()) {
            return false;
        }

        Q_DAL_Client::instance()->call('Admin_Home', 'Save', $param);

        return true;
    }

    public static function showMsg($param = array(), $status = self::STATUS_DANGER, $timeout = 2, $callbackUrl = '')
    {
        $params = array(
            'msg'         => '',
            'status'      => self::STATUS_DANGER,
            'timeout'     => 2,
            'callbackUrl' => Q_Http::getReferer(),
        );

        if (is_string($param)) {
            $param = array(
                'msg'     => $param,
                'status'  => $status,
                'timeout' => $timeout,
            );
            if (!empty($callbackUrl)) {
                $param['callbackUrl'] = $callbackUrl;
            }
        }

        $params = array_merge($params, $param);

        $currUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $length  = strpos($currUrl, '&jumpUrl=');
        if (!empty($length)) {
            $_jumpUrlBase64        = substr($currUrl, ($length + 9));
            $params['callbackUrl'] = base64_decode($_jumpUrlBase64);
        }

        if (Q_Request::resolveType() == Q_Request::CLI) {
            echo join(PHP_EOL, (array)$params['msg']) . PHP_EOL;
            Q::end();
        }
        /**
         * @var $output Q_Response
         */
        $output = Q_Registry::get('Q_Response');

        if (Q_Request::resolveType() == Q_Request::AJAX) {
            $output->jsonReturn('', $params['status'] == self::STATUS_SUCCESS ? Q_Response::STATUS_SUCCESS : Q_Response::STATUS_ERROR, $params['msg']);
        }

        $html = $output->fetchCol('Plugin/ShowMsg', $params, true);
        echo $output->fetchCol('Layout/Default', array('_content' => $html));
        Q::end();
    }
}
?>
